package com.qzsoft.zhjd.server.modules.ludong.asphalt.controller;

import com.qzsoft.zhjd.server.common.utils.MapToBeanUtils;
import com.qzsoft.zhjd.server.common.utils.R;
import com.qzsoft.zhjd.server.modules.ludong.asphalt.service.AsphaltConcreteCollectionService;
import com.qzsoft.zhjd.server.modules.ludong.asphalt.entity.AsphaltConcreteCollectionEntity;
import com.qzsoft.zhjd.server.modules.ludong.asphalt.handler.LudongAsphaltHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 功能描述:沥青混凝土数据采集
 * @author : liugf
 * @date : 2020/6/9 8:48
 */
@Slf4j
@RestController
@RequestMapping("datacollect/asphalt/asphaltconcretecollection")
public class AsphaltConcreteCollectionController {

    @Autowired
    private AsphaltConcreteCollectionService asphaltConcreteCollectionService;

    @Autowired
    private LudongAsphaltHandler ludongAsphaltHandler;

    /**
     * 功能描述:列表查询
     * @param  params 查询参数
     * @return : java.util.List
     * @author : liugf
     * @date : 2020/6/9 8:48
     */
    @GetMapping("/list")
    public R queryList(@RequestParam Map<String,Object> params){
        List<AsphaltConcreteCollectionEntity> list= asphaltConcreteCollectionService.list();
        return R.ok(list);
    }

    /**
     * 功能描述:保存
     * @param  params
     * @return :R
     * @author : jiangyf
     * @date : 2020/6/9 11:00
     */
    @PostMapping("/save")
    public R save(@RequestParam Map<String,Object> params){
        log.info("沥青混凝土数据采集==="+params.toString());
        try{
            Map<String,Object> map = MapToBeanUtils.toReplaceKeyLow(params);
            AsphaltConcreteCollectionEntity entity = MapToBeanUtils.mapToObject(map,AsphaltConcreteCollectionEntity.class);
            if (StringUtils.isNotBlank(entity.getId())){
                asphaltConcreteCollectionService.insertAndRemoveData(entity);

                // TODO
                ludongAsphaltHandler.insert(entity, "JD01AC1");
                return R.ok();
            }
            return R.error(400,"数据为空或id为空！");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return R.error(400,e.getMessage());
        }

    }


}
